const app = getApp();
const AJAX = app.$ajax,
  $wx = app.$wx;

Page({
  data: {
    detailedAddress: ''
  },
  
  onLoad(options){
    if(options.type){
      this.setData({
        perfectionType: parseInt(options.type)
      });
    }

    if(options.value){
      this.setData({
        inputVal: options.value
      })
    }
  },

  onShareAppMessage(){

  },

  getAddress(){
    wx.chooseLocation({
      success(res){
       
      }
    });
  },

  formSubmit(e){
    let td = this.data,
     form_obj = e.detail.value,
     params = {};
     
    switch (td.perfectionType){
      case 1:
        if(!form_obj.name){
          $wx.showToast('请输入姓名');
          return false
        }
        params.field = 'nickname';
        params.value = form_obj.name;
        break;

      case 2:
        if(!form_obj.mobile){
          $wx.showToast('请输入手机号');
          return false
        }else if (!$wx.checkPhone(form_obj.mobile)){
          $wx.showToast('请输入正确的手机号');
          return false
        }
        params.field = 'mobile';
        params.value = form_obj.mobile;
        break;

      case 3:
        if(!form_obj.address){
          $wx.showToast('请输入地址');
          return false
        }
        params.field = 'address';
        params.value = form_obj.address;
        break;
    }
    
    params.value !== td.inputVal && this.setUserInfo({
      field: params.field,
      value: params.value
    });
  },

  /**
   * 设置用户信息
   * @param field 需要修改的字段
   * @param value 修改的值
   * */
  setUserInfo(params) {
    let _this = this;
    params.loadType = 'default';
    params.loadMsg = '修改中...';
    AJAX.setUserInfo(params).then(([status, res, msg]) => {
      $wx.showToast(msg, {
        success() {
          if (status) {
            wx.navigateBack();
          }
        }
      });
    });
  }
});